from flask import Blueprint,request,jsonify
bp_reny = Blueprint("reny",__name__)
from db import db1
from comm import get_data

@bp_reny.route("/buM",methods=['POST'])
def buM():
    data = get_data()
    sql = "insert into branch values(0,'%s')"%(data['name'])
    db1.insert(sql)
    return jsonify({'code':200})


@bp_reny.route("/yuanM",methods=['GET','POST','DELETE'])
def yuanM():
    if request.method == 'GET':
        id = request.args.get('id')
        sql = "select * from staff where id=%d"%(int(id))
        rse = db1.find(sql)
        return jsonify({'code':200,'staf':rse})

    elif request.method == 'POST':
        id = request.args.get('id')
        data = get_data()
        if id:
            sql = "update staff set name='%s',gender=%d,salary=%d,br_id=%d"%(data['name'],int(data['gender']),int(data['salary']),int(data['br_id']))
            db1.insert(sql)
            return jsonify({'code':200})
        else:
            sql = "insert into staff values(0,'%s',%d,%d,%d)"%(data['name'],int(data['gender']),int(data['salary']),int(data['br_id']))
            db1.insert(sql)
            return jsonify({'code':200})
    
    elif request.method == 'DELETE':
        id = request.args.get('id')
        sql = "delete from staff where id=%d"%(int(id))
        db1.insert(sql)
        return jsonify({'code':200})